// 管理医疗点账号，实现增加和删除，需要root权限
<template>
    <el-form :inline="true" :model="accountForm">
        <el-form-item label="用户名">
            <el-input v-model="accountForm.username" />
        </el-form-item>
        <el-form-item label="密码">
            <el-input v-model="accountForm.password" />
        </el-form-item>
        <el-form-item label="地址">
            <el-input v-model="accountForm.address" />
        </el-form-item>
        <el-form-item label="电话">
            <el-input v-model="accountForm.phone" />
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="onSubmit">添加</el-button>
        </el-form-item>
    </el-form>
    <el-table v-loading="loading" :data="accounts" :default-sort="{ prop: 'create_time', order: 'descending' }"
        height="650px">
        <el-table-column prop="username" sortable label="用户名" />
        <el-table-column prop="create_time" sortable label="创建时间" />
        <el-table-column prop="address" label="地址" />
        <el-table-column prop="phone" label="电话" />
        <el-table-column label="操作">
            <template v-slot="scope">
                <el-button type="danger" @click="deleteUser(scope.row.username)">删除用户</el-button>
            </template>
        </el-table-column>
    </el-table>
</template>

<script>
import { Request } from '../utils/request'
import { ElMessage } from 'element-plus'

export default {
    name: 'PMSAccount',
    data() {
        return {
            accountForm: {
                username: '',
                password: '',
                address: '',
                phone: ''
            },
            accounts: [],
            loading: false
        };
    },
    created() {
        this.reload()
    },
    methods: {
        reload() {
            this.loading = true
            Request({
                url: '/account',
                method: 'get'
            }).then(res => {
                this.accounts = res.account_list
                this.loading = false
                console.log(this.loading)
            })
        },
        onSubmit() {
            ElMessage({ message: '上传中……' })
            let data = this.accountForm
            data.create_time = new Date().toISOString().slice(0, 10)
            Request({
                url: '/account',
                method: 'post',
                data: data
            }).then(res => {
                if (res === 'success') {
                    ElMessage({
                        message: '添加成功',
                        type: 'success'
                    })
                    this.accountForm = { username: '', password: '' }
                    this.reload()
                }
                else {
                    ElMessage({
                        message: '错误：' + res,
                        type: 'error'
                    })
                }
            })
        },
        deleteUser(name) {
            Request({
                url: '/account',
                method: 'delete',
                data: { username: name }
            }).then(res => {
                if (res === 'success') {
                    ElMessage({
                        message: '删除成功',
                        type: 'success'
                    })
                    this.reload()
                }
                else {
                    ElMessage({
                        message: '错误：' + res,
                        type: 'error'
                    })
                }
            })
        }
    }
}
</script>